import {
    Inject, Controller, Post,
    Body,
    Get
} from '@midwayjs/core';
import { Context } from '@midwayjs/koa';
import { AuthService } from '../service/auth.service';
import { User } from '../entity/user.entity';
import { CaptchaService } from '@midwayjs/captcha';

@Controller('/auth')
export class UserController {
    @Inject()
    ctx: Context;

    @Inject()
    authService: AuthService;

    @Inject()
    captchaService: CaptchaService;
    @Get('/captcha')
    async getCaptcha() {
        const { id, imageBase64 } = await this.captchaService.image({ width: 100, height: 40 })
        return {
            code: 200,
            data: {
                id,
                code: imageBase64
            }
        }
    }

    @Post('/login')
    async login(@Body() user: Partial<User>) {
        try {
            const result = await this.authService.login(user);

            return {
                code: 200,
                message: '',
                data: result
            };
        } catch (error) {
            return {
                code: 500,
                message: error.message || '服务内部错误'
            };
        }
    }
} 